<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

$act = $_G['gp_act'];

if( $act == 'get_sources' ){
	$sources = customer_source();
	$orderby = $_G['gp_orderby'];

	$date = date('Y-m-d');
	$firstday = date('Y-m-01', strtotime("$date -1 month -1 day"));
	$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));

	$begin_time = strtotime("last Sunday +1 day");
	$end_time = strtotime("next Sunday");

	if( $orderby == 'last_week' ){
		$begin_time = strtotime("last Sunday -6 day");
		$end_time = strtotime("last Sunday +1 day");
	}else if( $orderby == 'month' ){
		$date = date('Y-m-d');
		$firstday = date('Y-m-01', strtotime($date));
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
		$begin_time = strtotime($firstday);
		$end_time = strtotime($lastday);
	}else if( $orderby == 'last_month' ){
		$date = date('Y-m-d');
		$firstday = date('Y-m-01', strtotime("$date -1 month -1 day"));
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
		$begin_time = strtotime($firstday);
		$end_time = strtotime($lastday);
	}

	$where = array(
		"`create_time` > {$begin_time}",
		"`create_time` < {$end_time}",
	);
	$all_count = DB::Count('customer', $where);
	$source_count = DB::Count('customer', $where, '', 'source');
	
	$colors = array(
		'68BC31', '2091CF', 'AF4E96', 'DA5430', 'FEE074', '1b9299', '5e800b', '370dc0', '140f23', 'da235f', 'ec3200', '48ec00', 'dc9f1c'
	);

	$datas = array();

	foreach( $source_count as $k => $v ){
		$data = array(
			'label' => $sources[$v['source']] ? $sources[$v['source']] : '未知',
			'data' => (float)moneyit(($v['count'] / $all_count) * 100),
			'color' => "#{$colors[$k]}",
		);
		$datas[] = $data;
	}
	$json = array(
		'datas' => $datas,
		'date' => date('Y-m-d', $begin_time) . '-' . date('Y-m-d', $end_time)
	);
	jsonp($json, 'data', 0);
}else if( $act == 'get_status_chars' ){
	$status = customer_status();
	$begin_time = strtotime(date('Y-01-01'));
	$where = array(
		"`update_time` > {$begin_time} OR `create_time` > {$begin_time}"
	);
	$options = array(
		'order' => 'ORDER BY `create_time` DESC',
		'select' => '`id`, `status`, `update_time`, `create_time`'
	);
	$customers = limitquery('customer', $where, $options);
	$datas = array();
	foreach( $customers as $k => $v ){
		$datas[$status[$v['status']]][abs(date('m', $v['update_time']))]++;
	}

	$json = array(
		'datas' => $datas,
	);
	jsonp($json, 'data', 0);
}else if( $act == 'get_salers' ){
	$status = customer_status();
	$companys = parse_array(limitquery('companys'));
	$date = date('Y-m-d');
	$firstday = date('Y-m-01', strtotime($date));
	$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
	$begin_time = strtotime($firstday);
	$end_time = strtotime($lastday);
	$salers = array();
	foreach( $admincp->admins as $k => $v ){
		if( $v['departmentid'] == 3 ){
			$salers[$v['id']] = $v;
		}
	}
	
	$where = array(
		"`create_time` > {$begin_time} AND `adminid` > 0"
	);
	$customers = limitquery('customer', $where, array(
		'select' => '`id`, `adminid`, `status`'
	));
	foreach( $salers as $k => $v ){
		foreach( $status as $sk => $sv ){
			$salers[$k]['customers'][$sv] = 0;
		}
	}
	foreach( $customers as $k => $v ){
		if( !$salers[$v['adminid']] ){
			$salers[$v['adminid']] = $admincp->admins[$v['adminid']];
		}
		$salers[$v['adminid']]['customers'][$status[$v['status']]]++;
		$companys[$salers[$v['adminid']]['companyid']]['sum']++;
		$companys[$salers[$v['adminid']]['companyid']]['customers'][$status[$v['status']]]++;
	}

	$datas = array();
	$companysids = array();
	foreach( $salers as $k => $v ){
		$v['avatar'] = avatar($v['avatar']);
		$v['sum'] = array_sum($v['customers']);
		$datas[$v['id']] += $v['sum'];
	}
	array_multisort($datas, SORT_DESC, $salers);
	$json = array(
		'count' => count($customers),
		'datas' => $salers,
		'companys' => $companys
	);
	jsonp($json, 'data', 0);
}
die;

?>